<?php
/**
 * Base class for a TwitterStatusFeed
 * Fetches tweets for a given query
 *
 */
class TwitterStatusFeed extends ArrayIterator 
{
  protected $_query = null;
  protected $_tweets = array();
  
  public function __construct(TwitterStatusQuery $query)
  {
    $this->setQuery($query);
    
    parent::__construct($this->_tweets);
  }
  
  /**
   *
   * @return TwitterStatusQuery
   */
  public function getQuery()
  {
    return $this->_query;
  }
  
  public function setQuery(TwitterStatusQuery $query)
  {
    $this->_query = $query;
  }
  
  public function fetchTweets()
  {
    $query = $this->getQuery();
    $query->setIncludeEntities(true);
    
    $browser = new sfWebBrowser();
    $browser->get($query->buildUrl());
    
    $tweets = array();
    foreach ($browser->getResponseDomCssSelector()->matchAll('statuses > status') as $node)
    {
      $tweet = new TwitterTweetWrapper();
      $tweet->setDomDocument($node);
      $tweet->parseText();
      $tweets[$tweet->getId()] = $tweet;
    }
    
    $this->_tweets = array_merge($this->_tweets, $tweets);
    
    return $tweets;
  }
  
  public function getTweets()
  {
    return $this->_tweets;
  }
}
